package com.yinhai.message.search.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.util.ValidateUtil;
import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.search.vo.MessageInfoVo;
/**
 * 
 * @author yanglq
 * 2017.4.10
 */
@Repository("messageSearchDao")
public class MessageSearchDaoImpl extends HibernateDAO<MessageInfoVo> implements IMessageSearchDao{

	@Override
	public List<MessageInfoVo> getMessage(String systemid, String userid, int num,String messageType) {
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
		   .append(" m1.msgid,m1.senduserid,m1.sname,m1.msgdate,m1.title,m1.msgcontent,m2.read ")
		   .append(" ,m1.selfType,m1.otherData1,m1.otherData2,m1.otherData3 ) ")
		   .append(" FROM MessagePo m1,MessageDetailPo m2  ")
		   .append(" where m2.id.userid = ? ")
		   .append(" and m2.id.systemid= ? ")
		   .append(" and m2.id.sendertype= ? ")
		   .append(" and m1.msgid = m2.id.mgid ")
		   .append(" and to_char(sysdate,'yyyy-mm')=to_char(m1.msgdate,'yyyy-mm') ")
		   .append(" and (m2.read='0' or to_char(sysdate,'yyyy-mm-dd')=to_char(m1.msgdate,'yyyy-mm-dd')) ");
		hql.append(" order by m1.msgdate desc ");
		if(num>0){
			IPage<MessageInfoVo> page = super.selectFromMultiTableWithPage(hql.toString(), 0, num, "dwr", userid,systemid,messageType);
			return page.getList();
		}else{
			return super.selectFromMultiTable(hql.toString(),userid,systemid,messageType);
		}
	}
	
	@Override
	public List<MessageInfoVo> getMessage(String systemid, String userid,String pageid, int num,String messageType) {
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
		   .append(" m1.msgid,m1.senduserid,m1.sname,m1.msgdate,m1.title,m1.msgcontent,m2.read ")
		   .append(" ,m1.selfType,m1.otherData1,m1.otherData2,m1.otherData3 ) ")
		   .append(" FROM MessagePo m1,MessageDetailPo m2  ")
		   .append(" where m2.id.userid = ? ")
		   .append(" and m2.id.systemid= ? ")
		   .append(" and m2.id.sendertype= ? ")
		   .append(" and m1.msgid = m2.id.mgid ")
		   .append(" and to_char(sysdate,'yyyy-mm')=to_char(m1.msgdate,'yyyy-mm') ")
		   .append(" and (m1.otherData1= ? or COALESCE(otherdata1,'ABC')='ABC' ) ")
		   .append(" and (m2.read='0' or to_char(sysdate,'yyyy-mm-dd')=to_char(m1.msgdate,'yyyy-mm-dd')) ");
		hql.append(" order by m1.msgdate desc ");
		if(num>0){
			IPage<MessageInfoVo> page = super.selectFromMultiTableWithPage(hql.toString(), 0, num, "dwr", userid,systemid,messageType,pageid);
			return page.getList();
		}else{
			return super.selectFromMultiTable(hql.toString(),userid,systemid,messageType,pageid);
		}
	}

	@Override
	public List<MessageInfoVo> getPollMessage(String systemid, String userid, int num, String messageType) {
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
				.append(" m1.msgid,m1.senduserid,m1.sname,m1.msgdate,m1.title,m1.msgcontent,m2.read ")
				.append(" ,m1.selfType,m1.otherData1,m1.otherData2,m1.otherData3 ) ")
				.append(" FROM MessagePo m1,MessageDetailPo m2  ")
				.append(" where m2.id.userid = ? ")
				.append(" and m2.id.systemid= ? ")
				.append(" and m2.id.sendertype= ? ")
				.append(" and m1.msgid = m2.id.mgid ")
				.append(" and to_char(sysdate,'yyyy-mm')=to_char(m1.msgdate,'yyyy-mm') ")
				.append(" and m2.read='0' ");
		hql.append(" order by m1.msgdate desc ");
		if(num>0){
			IPage<MessageInfoVo> page = super.selectFromMultiTableWithPage(hql.toString(), 0, num, "dwr", new Object[]{userid,systemid,messageType});
			return page.getList();
		}else{
			return super.selectFromMultiTable(hql.toString(),userid,systemid,messageType);
		}
	}

	@Override
	public List<MessageInfoVo> getPollMessage(String systemid, String userid, String pageid, int num, String messageType) {
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
				.append(" m1.msgid,m1.senduserid,m1.sname,m1.msgdate,m1.title,m1.msgcontent,m2.read ")
				.append(" ,m1.selfType,m1.otherData1,m1.otherData2,m1.otherData3 ) ")
				.append(" FROM MessagePo m1,MessageDetailPo m2  ")
				.append(" where m2.id.userid = ? ")
				.append(" and m2.id.systemid= ? ")
				.append(" and m2.id.sendertype= ? ")
				.append(" and m1.msgid = m2.id.mgid ")
				.append(" and to_char(sysdate,'yyyy-mm')=to_char(m1.msgdate,'yyyy-mm') ")
				.append(" and (m1.otherData1= ? or COALESCE(otherdata1,'ABC')='ABC' ) ")
				.append(" and m2.read='0' ");
		hql.append(" order by m1.msgdate desc ");
		if(num>0){
			IPage<MessageInfoVo> page = super.selectFromMultiTableWithPage(hql.toString(), 0, num, "dwr", userid,systemid,messageType,pageid);
			return page.getList();
		}else{
			return super.selectFromMultiTable(hql.toString(),userid,systemid,messageType,pageid);
		}
	}

	@Override
	public void updateMessageRead(String mgid, String systemid, String userid,String messageType) {
		StringBuilder hql = new StringBuilder();
		hql.append(" UPDATE MessageDetailPo m2 SET read = '1' ")
		   .append(" WHERE m2.id.mgid = ?")
		   .append(" AND m2.id.userid = ?")
		   .append(" AND m2.id.systemid = ?")
		   .append(" and m2.id.sendertype= ? ");
		update(hql.toString(), mgid,userid,systemid,messageType);
	}

	@Override
	public List<MessageInfoVo> queryMessage(Map<String, Object> map) {
		Map<String, Object> param = new HashMap<>();
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
		   .append(" m1.msgid,m1.senduserid,m1.sname,m1.msgdate,m1.title,m1.msgcontent,m2.read ")
		   .append(" ,m1.selfType,m1.otherData1,m1.otherData2,m1.otherData3 ) ")
		   .append(" FROM MessagePo m1,MessageDetailPo m2  ")
		   .append(" where m2.id.userid = :userid ")
		   .append(" and m2.id.systemid= :systemid ")
		   .append(" and m2.id.sendertype= :sendertype ")
		   .append(" and m1.msgid = m2.id.mgid ")
		   .append(" and m1.msgdate>=to_date(:beginDate,'YYYY-MM-DD') and m1.msgdate<=to_date(:endDate,'YYYY-MM-DD') ");
		if(ValidateUtil.isEmpty(map.get("sendertype"))){
			map.put("sendertype", MessageType.DWR);
		}
		param.put("userid", map.get("userid"));
		param.put("systemid", map.get("systemid"));
		param.put("sendertype", map.get("sendertype"));
		param.put("beginDate", map.get("beginDate"));
		param.put("endDate", map.get("endDate"));
		if(!ValidateUtil.isEmpty(map.get("read"))){
			hql.append(" and m2.read=:read ");
			param.put("read", map.get("read"));
		}
		if(!ValidateUtil.isEmpty(map.get("selfType"))){
			hql.append(" and m1.selfType=:selfType ");
			param.put("selfType", map.get("selfType"));
		}
		if(!ValidateUtil.isEmpty(map.get("sendUserid"))){
			hql.append(" and m1.senduserid=:sendUserid ");
			param.put("sendUserid", map.get("sendUserid"));
		}
		if(!ValidateUtil.isEmpty(map.get("title"))){
			hql.append(" and m1.title like :title  ");
			param.put("title", "%"+map.get("title")+"%");
		}
		
		hql.append(" order by m1.msgdate desc ");
		return super.selectFromMultiTable(hql.toString(),param);
	}

	@Override
	public List<MessageInfoVo> queryAppMessage(Map<String, Object> map) {
		Map<String, Object> param = new HashMap<>();
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
		   .append(" m1.mgid,m1.aac001,m1.mgdate,m1.title,m1.mgcontent )")
		   .append(" FROM AppMessagePo m1  ")
		   .append(" where m1.aac001 = :aac001 ")
		   .append(" and mgdate>=to_date(:beginDate,'YYYY-MM-DD') and mgdate<=to_date(:endDate,'YYYY-MM-DD') ");  
		param.put("aac001", map.get("aac001"));
		param.put("beginDate", map.get("beginDate"));
		param.put("endDate", map.get("endDate"));
		return super.selectFromMultiTable(hql.toString(),param);
	}

	@Override
	public IPage<MessageInfoVo> queryMessageForPage(String systemid, String userid, String messageType, int page, int limit) {
		StringBuilder hql = new StringBuilder();
		hql.append(" SELECT new com.yinhai.message.search.vo.MessageInfoVo( ")
				.append(" m1.msgid,m1.senduserid,m1.sname,m1.msgdate,m1.title,m1.msgcontent,m2.read ")
				.append(" ,m1.selfType,m1.otherData1,m1.otherData2,m1.otherData3 ) ")
				.append(" FROM MessagePo m1,MessageDetailPo m2  ")
				.append(" where m2.id.userid = ? ")
				.append(" and m2.id.systemid= ? ")
				.append(" and m2.id.sendertype= ? ")
				.append(" and m1.msgid = m2.id.mgid ")
				.append(" and to_char(sysdate,'yyyy-mm')=to_char(m1.msgdate,'yyyy-mm') ");
		hql.append(" order by m2.read asc,m1.msgdate desc ");

		return 	super.selectFromMultiTableWithPage(hql.toString(), (page-1)*limit, limit, "dwr", userid, systemid, messageType);
	}
}
